<?php
	include 'head.php';
	//获取商品信息
	$id = $_GET['gid'];
	$cate = $_GET['cid'];

	//查询商品详细信息
	$sql = "select * from ".PRE."goods where id='{$id}'";
	$goods = mysql_fetch_assoc(mysql_query($sql));
	//查询详情页如果没有数据，直接跳转到首页
	if(empty($goods)){
		echo '<meta http-equiv="Refresh" content="0; url=index.php"/> ';
		exit;
	}
	
	//更新浏览数
	$sql = "update ".PRE."goods set views = views+1 where id='{$id}'";
	mysql_query($sql);
	
	//查询推荐商品列表
	$sql = "select cate_id from ".PRE."homepage where recommend='1'";
	$cate = mysql_fetch_assoc(mysql_query($sql));
	$sql = "select id,cate_id,image from ".PRE."goods where is_up='1' and cate_id in(select id from mbs_category where path like '%,{$cate[cate_id]},%') || cate_id = '{$cate[cate_id]}' order by addtime desc limit 10";
	$result = mysql_query($sql);
	$goodslist = array();
	while($row = mysql_fetch_assoc($result)){
		$goodslist[] = $row;
	}
	mysql_free_result($result);
	
	//查询商品详情图
	$sql = "select name from ".PRE."image where goods_id='{$id}'";
	$result = mysql_query($sql);
	while($row = mysql_fetch_assoc($result)){
		$imagelist[] = $row;
	}
	
	//评论分页
	//获取总记录数
    $sql = "select count(id) num from ". PRE ."comment where goods_id='{$id}'";
    
    $total = mysql_fetch_assoc(mysql_query($sql));
    $total = $total['num']; //总记录数

    //每页显示数
    $num = 10;

    //总页数
    $amount = ceil($total / $num);

    //页码
    $page = empty($_GET['p'])? 1 : (int)$_GET['p'];

    if($page < 1){
        $page = 1;
    }

    if($page > $amount){
        $page = $amount;
    }

    //偏移量
    $offset = ($page - 1) * $num;  
	
	
	//获取用户评论
	$sql = "select * from ".PRE."comment where goods_id='{$id}' and display='1' order by addtime desc limit {$offset},{$num}";
	$result = mysql_query($sql);
	while($row = @mysql_fetch_assoc($result)){
		$comlist[] = $row;
	}
	
	//制作分页按钮
 $start = ($page - 3) <= 1 ? 1 : $page - 3;

    $end = ($page + 3) >= $amount ? $amount : $page + 3 ;

    for($i = $start; $i <= $end; $i++){
        if($i == $page){
            $str .= '<li><a style="color:#E50065;font-weight:bold;" href="detail.php?gid='.$id.'p='. $i .'#comment" >['. $i .']</a></li>';
            continue;
        }
            $str .= '<li><a href="detail.php?gid='.$id.'&p='. $i .'#comment">['. $i .']</a><li>';

    }

    $nav = <<<tag
        <li><a href="detail.php?gid={$id}&p=1#comment">首页</a></li>
        $str
        <li><a href="detail.php?gid={$id}&p=$amount#comment">尾页</a></li>
tag;
	
?>

<div id="detail">
	<div class="detail_main w1">
		<div class="top">
			<div class="left fl">
			<img src="./upload/<?php echo $goods['image']?>" title="<?php echo $goods['id']?>" width="420"/>
			</div>
			<div class="right fr">
				<div class="name"><span><?php echo $goods['name']?></span></div>
				<div class="price"><img src="./images/icon.jpg" alt="" /><span class="new">折扣价：￥ <?php echo $goods['price']?></span><span>原价：￥ <?php echo ceil(($goods['price']/0.7))?></span></div>
				<div class="user">
					<span>用户评分</span>
					<img src="./images/xx_s3.jpg" alt="" />
					<img src="./images/xx_s3.jpg" alt="" />
					<img src="./images/xx_s3.jpg" alt="" />
					<img src="./images/xx_s3.jpg" alt="" />
					<img src="./images/xx_s3.jpg" alt="" />
					<div class="fr pinglun">
						<span>（已有
						<a class="fontyh"><?php echo $goods['views']?></a>
						人浏览）</span>
					</div>
				</div>
				<div class="buy">
					<form action="cart_action.php?a=add" method="post">
						<input type="hidden" name="id" value="<?php echo $id?>" />
						<input type="hidden" name="store" value="<?php echo $goods['store']?>" />
						<input type="hidden" name="price" value="<?php echo $goods['price']?>" />
						<input type="hidden" name="name" value="<?php echo $goods['name']?>" />
						<input type="hidden" name="image" value="<?php echo $goods['image']?>" />
						<span>数量：<input type="text" name="num" value="1"/></span><span>库存：<?php echo $goods['store']?></span>
						<div><input class="submit" type="submit" value="加入购物车" /></div>
					</form>
				</div>
			</div>
			<div class="clear"></div>
		</div>
		<!-- 下半截开始了 -->
		<div class="bottom">
			<div class="left fl">
				<ul>
					<?php foreach($goodslist as $val):?>
					<li><a href="detail.php?gid=<?php echo $val['id']?>&cid=<?php echo $val['cate_id']?>"><img src="./upload/<?php echo $val['image']?>" alt="" width="190"/></a></li>
					<?php endforeach;?>
				</ul>
			</div>
			<div class="right fr">
				<div class="description">
					<ul class="fontyh fl submenu">
						<li class="fl"><a class="checked" href="#abc">商品详情</a></li>
						<li class="fl"><a href="#chima">尺码对照</a></li>
						<li class="fl"><a href="#comment">顾客评价<span>(<?php echo $total?>)</span></a></li>
						<li class="fl"><a href="#pay">如何支付</a></li>
						<li class="fl"><a href="#tuihuo">30天无条件退货</a></li>
						<div class="clear"></div>
					</ul>
					<ul class="shuoming">
						<li><img src="./images/2013-11-18_192941.png" /></li>
						<?php if(!empty($imagelist)): foreach($imagelist as $val):?>
						<li><img src="./upload/<?php echo $val['name']?>" /></li>
						<?php endforeach;endif;?>
						<li><a name="chima"></a><img src="./images/2013-12-11_130709.png" /></li>
						<li>
							<div class="ucindex">
								<div class="shopcar comment">
									<div class="mt20">
										<table cellspacing="0" cellpadding="0" width="780">
											<tr class="color">
												<a name="comment"></a>
												<th colspan="4">用户评论</th>
											</tr>
												<?php if(!empty($comlist)):?>
												<?php foreach($comlist as $tr):?>
												<tr class="goods" title="<?php echo $tr['id']?>">
													<td width="80"><?php 
													$sql = "select touxiang from ".PRE."user where name='{$tr['user_name']}'";
													$userinfo = mysql_fetch_assoc(mysql_query($sql));
													?>
													<div><img src="<?php echo APP.'/upload/user/'.$userinfo['touxiang']?>" height="50"/></div>
													<?php echo $tr['user_name'];?>
													</td>
													<td><?php echo $tr['content'] ?></td>
													<td width="120"><?php echo date('Y-m-d H:i:s',$tr['addtime']) ?></td>
												</tr>
												<?php endforeach;?>
												<?php else:?>
												<tr>
													<td colspan="3">没有找到任何记录</td>
												</tr>
												<?php endif;?>
											
										</table>
										<!-- 搜索分页按钮 -->
										<?php if($total > $num):?>
										<div class="searchnav">
											<div class="tips"><?php $start = $offset + 1>0? $offset+1: 0;$end = $offset + $num >= $total? $total : $offset + $num; echo "Showing $start to $end of $total entries";?></div>
											<ul class="user_nav">
												<?php echo $nav?>
											</ul>
										</div>
										<?php endif;?>
									</div>
								</div>
							</div>
						<li>
						<li><a name="pay"></a><img src="./images/pay.png" /></li>
						<li><a name="tuihuo"></a><img src="./images/2013-12-11_130800.png" /></li>
					</ul>
				</div>	
			</div>
			<div class="clear"></div>
		</div>
	</div>
</div>



<?php
	include 'footer.php';
?>
